const app = new Vue({
  el: '#app',
  data: {
    books: [
      {
        id: 1,
        name: '《算法导论》',
        date: '2006-9',
        price: 85.00,
        count: 1
      },
      {
        id: 2,
        name: '《UNIX编程艺术》',
        date: '2006-2',
        price: 59.00,
        count: 1
      }, {
        id: 3,
        name: '《编程珠玑》',
        date: '2008-10',
        price: 39.00,
        count: 1
      },
      {
        id: 4,
        name: '《编程大全》',
        date: '2006-9',
        price: 128.00,
        count: 1
      }
    ]
  },
  methods: {
    incr: function (item) {
      item.count++;
    },
    decr: function (item) {
      if (item.count > 1) {
        item.count--;
      }
    },
    isDecrDisabled: function (item) {
      return item.count <= 1;
    },
    removeHandler: function (index) {
      this.books.splice(index, 1);
    }
  },
  filters: {
    formatPrice(price) {
      return '￥' + price.toFixed(2);
    }
  },
  computed: {
    price: function () {
      return this.books.map(item => {
        return item.price * item.count;
      }).reduce((t1, t2) => {
        return t1 + t2;
      });
    }
  }
});